#include "BasicInfoModule.h"

QScopedPointer<BasicInfoModule> BasicInfoModule::ms_instance;
BasicInfoModule::BasicInfoModule(QObject *parent)
    :BaseModule(s_moduleName,parent)
    ,m_timer(new QTimer(this))
{
    connect(m_timer, &QTimer::timeout, this, &BasicInfoModule::dealInfo);
    connect(this , &BasicInfoModule::pollingIntervalChanged ,this, &BasicInfoModule::setTimerPollingInterval);
}

BasicInfoModule::~BasicInfoModule()
{

}

BasicInfoModule *BasicInfoModule::instance()
{
    if (ms_instance.isNull()) {
        ms_instance.reset(new BasicInfoModule());
    }
    return ms_instance.data();
}

void BasicInfoModule::dealInfo()
{
    BasicInfo basicInfo;
    QScopedPointer<BasicInfoCollection> basicInfoCollection(new BasicInfoCollection(this));
    basicInfoCollection->getInfo(basicInfo);
    QJsonObject sourceObj = sourceInfo();
    QJsonObject informationObj = packageBasicInfo(basicInfo);
    QJsonObject sendedInfoObj;
    QString sendedInfoStr;
    sendedInfoObj.insert(gs_informationKey,informationObj);
    sendedInfoObj.insert(gs_sourceKey,sourceObj);
    sendedInfoStr = QJsonDocument(sendedInfoObj).toJson(QJsonDocument::Compact);
    emit  sendInfo(sendedInfoStr);
    qInfo()<<"BasicInfoModule::dealInfo: save basic info";
}

void BasicInfoModule::setTimerPollingInterval(qlonglong sec)
{
    m_timer->stop();
    m_timer->setInterval(secToMsec(sec));
    m_timer->start();
}

QJsonObject BasicInfoModule::packageBasicInfo(const BasicInfo &basicInfo)
{
    QJsonObject basicInfoObject;
    basicInfoObject.insert(s_kernelVersionKey,basicInfo.kernelVersion);
    basicInfoObject.insert(s_gccVersionKey,basicInfo.gccVersion);
    basicInfoObject.insert(s_glibcVersiKey,basicInfo.glibcVersi);
    basicInfoObject.insert(s_gdbVersionKey,basicInfo.gdbVersion);
    basicInfoObject.insert(s_qt5VersionKey,basicInfo.qt5Version);
    basicInfoObject.insert(s_qt4VersionKey,basicInfo.qt4Version);
    basicInfoObject.insert(s_python3VersionKey,basicInfo.python3Version);
    basicInfoObject.insert(s_python2VersionKey,basicInfo.python2Version);
    basicInfoObject.insert(s_javaVersionKey,basicInfo.javaVersion);
    basicInfoObject.insert(s_grubVersionKey,basicInfo.grubVersion);
    basicInfoObject.insert(s_cupsVersionKey,basicInfo.cupsVersion);
    basicInfoObject.insert(s_libjbigVersionKey,basicInfo.libjbigVersion);
    basicInfoObject.insert(s_libpngVersionKey,basicInfo.libpngVersion);
    basicInfoObject.insert(s_libjpegKey,basicInfo.libjpeg);
    basicInfoObject.insert(gs_createTimeStampKey
        ,QDateTime::currentDateTime().toTimeZone(QTimeZone(gs_timezone)).toString(gs_timeFormat));
    basicInfoObject.insert(s_kylinDaqVersionKey,basicInfo.kylinDaqVersion);
    printInfo(basicInfo);
    return basicInfoObject;
}

QJsonObject BasicInfoModule::sourceInfo()
{
    QJsonObject sourceObj;
    QCryptographicHash cryHash(QCryptographicHash::Algorithm::Sha256);
    QString tidValidString = QString(s_packageNemeValue)+QString(s_messageTypeValue);
    cryHash.addData(tidValidString.toUtf8());
    QString nowTid(cryHash.result().toBase64());
    sourceObj.insert(gs_packageNameKey,s_packageNemeValue);
    sourceObj.insert(gs_messageTypeKey,s_messageTypeValue);
    sourceObj.insert(gs_sourceVersionKey,gs_sourceVersionValue);
    sourceObj.insert(gs_tidKey,nowTid);
    return sourceObj;
}

void BasicInfoModule::updateProperty(const QJsonObject &conf, const QJsonObject &defaultConf)
{
    if(defaultConf.contains(moduleName())
       && defaultConf[moduleName()].isObject()
      )
    {
        QJsonObject moduleConf;
        QJsonObject moduleDefaultConf = defaultConf[moduleName()].toObject();
        if(conf.contains(moduleName())
            && conf[moduleName()].isObject())
        {
            moduleConf = conf[moduleName()].toObject();
        }
        qlonglong value;
        if(getValidConfInt(moduleConf,moduleDefaultConf,s_pollingIntervalKey,value) == FunctionSeccess)
        {
            setPollingInterval(value);
            qInfo()<<"module="<<moduleName()<<",property="<<s_pollingIntervalKey<<":"<<value;
        }
        else
        {
            qCritical()<<"module="<<moduleName()<<",property="<<s_pollingIntervalKey<<":read conf fail";
        }
    }
    else
    {
        qCritical()<<"module="<<moduleName()<<":update property fail";
    }
}

void BasicInfoModule::printInfo(const BasicInfo &basicInfo)
{
    qDebug() << s_kernelVersionKey << basicInfo.kernelVersion;
    qDebug() << s_gccVersionKey << basicInfo.gccVersion;
    qDebug() << s_glibcVersiKey << basicInfo.glibcVersi;
    qDebug() << s_gdbVersionKey << basicInfo.gdbVersion;
    qDebug() << s_qt5VersionKey << basicInfo.qt5Version;
    qDebug() << s_qt4VersionKey << basicInfo.qt4Version;
    qDebug() << s_python3VersionKey << basicInfo.python3Version;
    qDebug() << s_python2VersionKey << basicInfo.python2Version;
    qDebug() << s_javaVersionKey << basicInfo.javaVersion;
    qDebug() << s_grubVersionKey << basicInfo.grubVersion;
    qDebug() << s_cupsVersionKey << basicInfo.cupsVersion;
    qDebug() << s_libjbigVersionKey << basicInfo.libjbigVersion;
    qDebug() << s_libpngVersionKey << basicInfo.libpngVersion;
    qDebug() << s_libjpegKey << basicInfo.libjpeg;
    qDebug() << s_kylinDaqVersionKey << basicInfo.kylinDaqVersion;
}
